from jina import Flow, Document, DocumentArray
from executor import MultiModalExecutor

# 创建 Flow
flow = None

def get_flow():
    global flow
    if flow is None:
        # 添加 grpc 配置
        flow = Flow(
            protocol='http', 
            port=12345,
            grpc_channel_options={
                'grpc.keepalive_time_ms': 30000,
                'grpc.keepalive_timeout_ms': 10000,
                'grpc.keepalive_permit_without_calls': True,
                'grpc.http2.max_pings_without_data': 0,
                'grpc.http2.min_time_between_pings_ms': 10000,
                'grpc.http2.min_ping_interval_without_data_ms': 5000,
            }
        ).add(
            uses=MultiModalExecutor,
            name='multimodal_executor',
            uses_with={'workspace': './workspace'},
            workspace='./workspace'
        )
        flow.start()
        print('Jina Flow started')
    return flow

def cleanup():
    global flow
    if flow is not None:
        flow.close()
        flow = None 